<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .router-link-active {
      background-color: rgb(218, 22, 142);
    }

    .router-link-exact-active {
      background-color: rgb(218, 38, 22);
    }
  </style>
</head>

<body>
  <div id="app">
    <!-- 跳转路由组件 -->
    <button @click="enterHome">到首页</button>
    <button @click="enterNews">到新闻页</button>
    <button @click="enterAbout">到关于我们页</button>
    <!-- 在 视图上 指定路由组件出口 -->
    <router-view></router-view>

  </div>
  <script src="./vue.js"></script>
  <script src="./vue-router.js"></script>
  <script>
    // 准备路由组件对象 为了注册路由组件
    const Home = {
      template: `
        <div>
          <h2>我是首页</h2>  
        </div>
      `
    }
    const News = {
      template: `
        <div>
          <h2>我是新闻页</h2>  
        </div>
      `
    }
    const About = {
      template: `
        <div>
          <h2>关于我们</h2>  
        </div>
      `
    }
    const NotFound = {
      template: `
        <div>
          <h2>404啦</h2>  
        </div>
      `
    }
    const Login = {
      template: `
        <div>
          <h2>登录页</h2>  
          <button @click="login">点击登录</button>
        </div>
      `,
      methods: {
        login(){
          setTimeout(()=>{
            const token = 'fdhewifegfie94re';
            localStorage.setItem('token', token);
            // 登录成功后 跳转到 首页
            if(this.$route.query.from) {
              this.$router.replace(this.$route.query.from)
            }else{
              this.$router.replace('/home')
            }
           
          },1000)
        }
      }
    }
    // 创建一个 router 对象
    const routes = [
      {
        path: '/',
        redirect: '/home'
      },
      {
        path: '/home',
        component: Home
      },
      {
        path: '/news',
        component: News
      },
      {
        path: '/about',
        component: About
      },
      {
        path: '/login',
        component: Login
      },
      {
        path: '*',
        component: NotFound
      }
    ]
    const router = new VueRouter({
      // routes 路由规则
      routes
    });
    // 全局前置守卫
    router.beforeEach((to, from, next) => {
      console.log(to);
      /* 
      做登录鉴权
      先判断是否去登录 去登录 直接放行
      判断缓存中是否有token
        有登录 放行
        没有 没有登录  滚去登录页
      */
      const token = localStorage.getItem('token');
      if (to.path === '/login') {
        next()
      } else {

        if (token) {
          next();
        } else {
          next({
            path: '/login',
            query: {
              from: to.path
            }
          })
        }

      }


    });
    // 将router对象 挂载到实例上
    const vm = new Vue({
      el: "#app",
      router,
      methods: {
        enterHome() {
          this.$router.push("/home")
        },
        enterNews() {
          this.$router.push("/news")
        },
        enterAbout() {
          this.$router.replace("/about")
        }
      }
    })
  </script>
</body>

</html>